﻿package com.sz.ticket.bmo.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.json.JSONSerializer;

import com.sz.ticket.bmo.ITicketBMO;
import com.sz.ticket.dao.ITicketDAO;

public class TicketBMO implements ITicketBMO{
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	public ITicketDAO getTicketDAO() {
		return ticketDAO;
	}

	public void setTicketDAO(ITicketDAO ticketDAO) {
		this.ticketDAO = ticketDAO;
	}

	protected ITicketDAO ticketDAO = null;
	
	@SuppressWarnings("unchecked")
	public long addTicket(Map map) throws Exception {
		return ticketDAO.addTicket(map);
	}
	
	@SuppressWarnings("unchecked")
	/**
	 * 可接受的条件：
	 * TICKET_ID STATUS CUST_TEL CUST_NAME BUSINESS_ID REGION CREATE_MANAGER LOCK_MANAGER
	 */
	public List queryTicket(Map map) throws Exception {
		return ticketDAO.queryTicket(map);
	}
	
	@SuppressWarnings("unchecked")
	public long queryTicketCount(Map map) throws Exception {
		return ticketDAO.queryTicketCount(map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryTicketPage(Map map) throws Exception {
		return ticketDAO.queryTicketPage(map);
	}
	
	@SuppressWarnings("unchecked")
	public String lockTickets(List list) throws Exception {
		if(list == null) return "参数错误，或参数转换错误";
		
		int nSuccess = 0;
		int nFail = 0;
		StringBuffer err = new StringBuffer();
		
		for(int n = 0; n < list.size(); ++n) {
			Map map = (Map)list.get(n);
			String msg = ticketDAO.lockTicket(map);
			if(msg != null) {
				err.append(msg);
				err.append("\n");
				++nFail;
			} else {
				++nSuccess;
			}
		}

		if(nFail == 0) {			
			return String.format("成功处理%s条订单", nSuccess);
		} else {
			return String.format("成功处理%s条订单，失败%s条\n详细信息:\n%s", nSuccess, nFail, err.toString());
		}
	}
	
	@SuppressWarnings("unchecked")
	public String cancelTicket(Map map) throws Exception {
		String msg = ticketDAO.cancelTicket(map);
		//if(msg == null) msg = "操作成功"; //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public Map editTicket(Map map) throws Exception {
		return ticketDAO.editTicket(map);
	}
	
	@SuppressWarnings("unchecked")
	public String nextStatus(Map map) throws Exception {
		String msg = ticketDAO.nextStatus(map);
		//if(msg == null) msg = "操作成功";  //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public String confirmfailedTicket(Map map) throws Exception {
		String msg = ticketDAO.confirmfailedTicket(map);
		//if(msg == null) msg = "操作成功"; //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessTickets(Map map) throws Exception {
		return ticketDAO.queryBusinessTickets(map);
	}
	
	@SuppressWarnings("unchecked")
	public long queryBusinessTicketsCount(Map map) throws Exception {
		return ticketDAO.queryBusinessTicketsCount(map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessTicketsPage(Map map) throws Exception {
		return ticketDAO.queryBusinessTicketsPage(map);
		
	}
	
	public void checkTicket_Business(Map map) throws Exception {
		ticketDAO.checkTicket_Business(map);
	}
	
	public void checkTicket_Custom(Map map) throws Exception {
		ticketDAO.checkTicket_Custom(map);
	}

	@SuppressWarnings("unchecked")
	public Map confirmTicket(Map map) throws Exception {
		return ticketDAO.confirmTicket(map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessRecommand(Map map) throws Exception {
		return ticketDAO.queryBusinessRecommand(map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryTicketLog(Map map) throws Exception {
		return ticketDAO.queryTicketLog(map);
	}
}
